﻿using System;
using System.Collections.Generic;
using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Libs;
using Yoozoo.Managers;
using Yoozoo.UI.YGUI;

namespace Gameplay.PVE.Survivor
{
    public class HudChatBubble : IClass
    {
        private BattleHudObject hudObject;

        private GameObject root;
        private YText text;
        private BattleUnit unit;
        private Animation animation;

        private string content;
        private RectTransform anchor;
        private int showType;

        public bool IsShow
        {
            get
            {
                if (hudObject == null)
                {
                    return false;
                }
                else
                {
                    return hudObject.Active;
                }
            }
        }

        public void Create(BattleUnit unit)
        {
            this.unit = unit;
            hudObject = BattlePoolManager.Instance.GetHud((int)EBattleResId.ChatBubbleHud);
            hudObject.SetPrefabReadyAction(o =>
            {
                o.transform.SetParent(unit.Transform);
                o.transform.localPosition = new Vector3(0, 2.3f, 0);
                o.transform.localScale = new Vector3(0.015f, 0.015f, 0.015f);
                anchor = o.transform.Find("bg/bubbleBg").GetComponent<RectTransform>();
                text = o.GetComponentInChildren<YText>();
                text.text = content;
                resizeText();
                UpdateShowType();
                /*animation = o.GetComponentInChildren<Animation>();
                if (animation != null)
                {
                    animation.Play();
                }*/
            });
        }

        public void UpdateShowType()
        {
            if (showType == 0)
            {
                anchor.localScale = new Vector3(-1, 1, 1);
                text.transform.localScale = new Vector3(-1, 1, 1);
            }
            else
            {
                anchor.localScale = new Vector3(1, 1, 1);
                text.transform.localScale = new Vector3(1, 1, 1);
            }
            anchor.anchoredPosition = Vector2.zero;
        }
        
        public void Show(string content,int type)
        {
            //icon.sprite = ResourceManager.Instance.GetSprite(title);
            hudObject.Forward = CameraManager.Instance.CameraForward;
            hudObject.Active = true;
            this.content = content;
            this.showType = type;
            if (text)
            {
                text.text = content;
                resizeText();
                UpdateShowType();
            }
        }

        void resizeText()
        {
            var fixedWidth = 256;
            var rectTransform = text.rectTransform;
            if (text)
            {
                TextGenerator tg = text.cachedTextGeneratorForLayout;
                rectTransform.sizeDelta = new Vector2(fixedWidth, rectTransform.sizeDelta.y);
                var size = rectTransform.sizeDelta;
                TextGenerationSettings settings = text.GetGenerationSettings(size);
                var height = tg.GetPreferredHeight(text.text, settings);
                var width = tg.GetPreferredWidth(text.text, settings);
                if (width >= fixedWidth)
                {
                    rectTransform.sizeDelta = new Vector2(fixedWidth, height);
                }
                else
                {
                    rectTransform.sizeDelta = new Vector2(width, height);
                }
            }
        }

        public void Hide()
        {
            hudObject.Active = false;
        }

        public void Destroy()
        {
            BattlePoolManager.Instance.ReleaseHud(hudObject);
        }

        public void OnReset()
        {
           
        }
    }
}
